﻿####
# Enable and Configure State Service
#
####

# Add Sharepoint pssnapin

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

# Set up a bunch of useful aliases...

Set-Alias psconfig ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\psconfig.exe")
Set-Alias stsadm ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\stsadm.exe")


Write-Host -ForegroundColor Yellow "Services: Enable State Service Application on $env:farmname."

$stateapp = Get-SPStateServiceApplication

if ($stateapp -eq $null){

	#Create the State Service Application
	Write-Host "Creating State Service Application"
	$serviceApp = New-SPStateServiceApplication -Name "State Service"

	#Create a State Service Database and Associate it with the Service Application
	New-SPStateServiceDatabase -Name "$env:DatabasePrefix-StateServiceDatabase" -ServiceApplication $serviceApp

	#Create a State Service Application Proxy and Associate it with the Service Application
	Write-Host "Creating State Service Proxy"
	New-SPStateServiceApplicationProxy -Name "State Service Proxy" -ServiceApplication $serviceApp -DefaultProxyGroup
} else {

Write-Host "State Service has already been configured for this farm : $env:farmname"
}

Write-Host -ForegroundColor Yellow "Services: Enable State Service Application on $env:farmname. : COMPLETED"